<?php
// +----------------------------------------------------------------------
// | 火凤凰CMS内容管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2018 http://cxbs.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 赵志广 <amdin@cxbs.net>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Adminbase;
use app\common\model\Advertising as cxModel;
use app\common\controller\Upload;
use think\Loader;

class Advertising extends Adminbase {
    protected $beforeActionList = [
        'btnyz'  =>  ['only'=>'index,add,edit'],
    ];
    protected function btnyz(){
        $btn = $this->btnauth('add,edit,del');
        $this->assign('btn',$btn);
    }
    public function index(){
        $cxmodel = new cxModel();
        $list = $cxmodel->order('addtime desc')->paginate('20');

        $this->assign([
           'list' => $list,
        ]);
        return view();
    }
    // 添加广告
    public function add(){
        $getdata = request()->param();
        if(empty($getdata['t']) || !in_array($getdata['t'],array('4','1','2','3'))){
            $this->error("请选择正确的广告类型",'index');
        }
        if(request()->isPost()){
            $data = input('post.');
            $data = datatrim($data);
            $validate = Loader::validate('Advertising');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $cxmodel = new cxModel();
            $upmodel = new Upload();
            $pirdir = 'ad/'.date("Ym");
            switch ($data['seetype']){
                case '1':
                    $conts = $data['cont'];
                    $conts['img'] = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($conts['img'],$pirdir);
                    $conts['img'] = $upmodel->editadd($conts['img']);
                    $data['cont'] = serialize($conts);
                    unset($conts);
                    break;
                case '2':
                    $newcont = array();
                    $conts = $data['cont'];
                    foreach ($conts as $v){
                        $v['img'] = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($v['img'],$pirdir);
                        $v['img'] = $upmodel->editadd($v['img']);
                        $newcont[] = $v;
                    }
                    $newcont = $this->listfield($newcont);
                    $data['cont'] = serialize($newcont);
                    unset($newcont);
                    break;
                case '3':
                    $conts = $data['cont'];
                    $conts['img'] = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($conts['img'],$pirdir);
                    $conts['img'] = $upmodel->editadd($conts['img']);
                    $data['cont'] = serialize($conts);
                    unset($conts);
                    break;
                case '4':
                    $data['cont'] = serialize($data['cont']);
                    unset($conts);
                    break;
            }
            if($cxmodel->allowField(true)->save($data)){
                $this->addlog('添加广告【'.$data['title'].'】成功');
                $this->success('添加广告【'.$data['title'].'】成功','index');
            }else{
                $this->error('添加广告【'.$data['title'].'】失败');
            }
        }
        $postdb['seetype'] = $getdata['t'];
        $this->assign('postdb',$postdb);
        $cxmodel = new cxModel();

        return view();
    }
    // 修改广告
    public function edit(){
        if(!request()->param('id')){
            $this->error('访问错误！');
        }
        $getdata = request()->param();
        $cxmodel = new cxModel();
        $upmodel = new Upload();
        $postdb = $cxmodel->where('id',$getdata['id'])->find();
        $postdb['cont'] = unserialize($postdb['cont']);
        $postdb['cont'] = $upmodel->editadd($postdb['cont'],false);
        if(request()->isPost()){
            $data = input('post.');
            $data = datatrim($data);
            $validate = Loader::validate('Advertising');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $cxmodel = new cxModel();
            $upmodel = new Upload();
            $pirdir = 'ad/'.date("Ym");
            switch ($data['seetype']){
                case '1':
                    $oldconts = $postdb['cont'];
                    $conts = $data['cont'];
                    if($conts['img'] != $oldconts['img']){
                        $conts['img'] = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($conts['img'],$pirdir);
                        $conts['img'] = $upmodel->editadd($conts['img']);
                        @unlink(ROOT_PATH.$oldconts['img']);
                    }
                    $data['cont'] = serialize($conts);
                    unset($conts);
                    unset($oldconts);
                    break;
                case '2':
                    $newcont = array();
                    $oldconts = $postdb['cont'];
                    $conts = $data['cont'];
                    //  获取旧图片组
                    foreach ($oldconts as $v){
                        $oldimg[] = $v['img'];
                    }
                    foreach ($conts as $v){
                        $newimg[] = $v['img'];
                    }
                    //  处理提交过来的图片组
                    foreach ($conts as $v){
                        if(in_array($v['img'],$oldimg)){
                            $newcont[] = $v;
                            continue;
                        }
                        $v['img'] = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($v['img'],$pirdir);
                        $v['img'] = $upmodel->editadd($v['img']);
                        $newcont[] = $v;
                    }
                    foreach ($oldimg as $v){
                        if(!in_array($v,$newimg)){
                            @unlink(ROOT_PATH.$v);
                        }
                    }
                    $newcont = $this->listfield($newcont);
                    $data['cont'] = serialize($newcont);
                    unset($oldconts);
                    unset($newimg);
                    unset($conts);
                    unset($newcont);
                    break;
                case '3':
                    $oldconts = $postdb['cont'];
                    $conts = $data['cont'];
                    if($conts['img'] != $oldconts['img']){
                        $conts['img'] = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($conts['img'],$pirdir);
                        $conts['img'] = $upmodel->editadd($conts['img']);
                        @unlink(ROOT_PATH.$oldconts['img']);
                    }
                    $data['cont'] = serialize($conts);
                    unset($conts);
                    unset($oldconts);
                    break;
                case '4':
                    $data['cont'] = serialize($data['cont']);
                    break;
            }
            if($cxmodel->allowField(true)->isUpdate(true)->save($data,['id'=>$data['id']])){
                $this->addlog('编辑广告【'.$data['title'].'】成功');
                $this->success('编辑广告【'.$data['title'].'】成功','index');
            }else{
                $this->error('编辑广告【'.$data['title'].'】失败');
            }
        }
        $this->assign('postdb',$postdb);
        return view('add');
    }
    //  处理广告排序
    public function listfield($data){
        $keysvalue = $new_array = array();
        foreach ($data as $k => $v){
            $keysvalue[$k] = $v['sort'];
        }
        arsort($keysvalue);
        reset($keysvalue);
        foreach ($keysvalue as $k => $v) {
            $new_array[$k] = $data[$k];
        }
        return $new_array;
    }
    //  删除
    public function del(){
        $cxmodel = new cxModel();
        $upmodel = new Upload();
        $deldata = cxModel::get(input('id'));
        $title = $deldata['title'];
        $cont = unserialize($deldata['cont']);
        switch ($deldata['seetype']){
            case '1':
                $img = $upmodel->editadd($cont['img'],false);
                @unlink(ROOT_PATH.$img);
                break;
            case '2':
                foreach ($cont as $v){
                    $img = $upmodel->editadd($v['img'],false);
                    @unlink(ROOT_PATH.$img);
                }
                break;
            case '3':
                $img = $upmodel->editadd($cont['img'],false);
                @unlink(ROOT_PATH.$img);
                break;
        }
        if($deldata->delete()){
            $this->addlog("删除广告【{$title}】成功！");
            $this->success("删除广告【{$title}】成功！");
        }else{
            $this->addlog("删除广告【{$title}】失败！");
            $this->success("删除广告【{$title}】失败！");
        }
        return;
    }
    //  修改显示状态
    public function see(){
        if(request()->isPost()){
            $data = input('post.');
            $see = cxModel::get((int)$data['cxbsid']);
            if($see['status'] == 1){
                cxModel::update(['id' => $see['id'],'status' => '0']);
                $this->addlog('隐藏广告【'.$see['title'].'】！');
                $this->success("隐藏广告【{$see['title']}】！");
            }elseif($see['status'] == 0){
                cxModel::update(['id' => $see['id'],'status' => '1']);
                $this->addlog('显示广告【'.$see['title'].'】！');
                $this->success("显示广告【{$see['title']}】！");
            }
        }
        $this->error('访问错误！');
    }

}
